package com.snap.awesomeserial.utils;

import java.io.IOException;

public class RootShellCmd {

    private static RootShellCmd rootShel;

    /**
     * 获取实例
     *
     * @return
     */
    public static RootShellCmd getInstance() {
        if (rootShel == null) {
            synchronized (RootShellCmd.class) {
                if (rootShel == null) {
                    rootShel = new RootShellCmd();
                }
            }
        }
        return rootShel;
    }

    public void hideNavigation() {
        exec("LD_LIBRARY_PATH=/vendor/lib:/system/lib service call activity 42 s16 com.android.systemui");
    }

    public void showNavigation() {
        exec("LD_LIBRARY_PATH=/vendor/lib:/system/lib am startservice -n com.android.systemui/.SystemUIService");
    }

    public void performance() {
        //瑞芯板子没有 cpu hotplug管理
        exec("echo performance > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor");
    }

    public void silentInstall(String apkPath) {
        exec("pm install -r " + apkPath);
    }

    public void escape() {
        exec("am start -n com.android.launcher3/.Launcher");
    }

    public void restart(String name) {
        exec("am force-stop " + name + "\nam start -n " + name + "/.ui.LoginActivity");
    }

    public void uninstall(String pkgName) {
        exec("pm uninstall --user 0 " + pkgName);
    }

    private void exec(String command) {
        new Thread(() -> execShell(command)).start();
    }

    public static void execShell(String command) {
        try {
            Process proc = Runtime.getRuntime().exec(new String[]{"su", "-c", command});
            int exitCode = proc.waitFor();
//                Logger.e("exec : "command + " , exitCode : " + exitCode);
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}
